/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package App1.bookShop.controllers;

import APP1.GUI2.MainFrame_2;
import APP1.bookShop.book.Book;
import APP1.bookShop.book.EBook;
import APP1.bookShop.main.BookShop;
import APP1.bookShop.people.Author;
import APP1.bookShop.people.Publisher;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author Asus
 */
public class PublisherController {
    
    private BookShop bookShop;
    
    public PublisherController(BookShop bookShop) {
        this.bookShop = bookShop;
    }
    
    public DefaultListModel getPublishersListModel() {
        DefaultListModel lm;
        
        lm = new DefaultListModel();
        
        for (Publisher a : bookShop.getPublishers()) {
            
            lm.addElement(a);
            
        }
        if (lm.getSize() <= 0) {
            lm.addElement("NO Publishers");
        }
        return lm;
    }
    
    public void handleDetailPublisher(int selected) {
        if (selected >= 0) {
            Publisher pub = bookShop.getPublishers().get(selected);
            if (pub != null) {
                MainFrame_2 GUI = bookShop.getGUI();
                GUI.getPublisherNameTextField().setText(pub.getName());
                GUI.getPublisherEmailTextField().setText(pub.getEmail());
                GUI.getPublisherPOstalCodeTextField().setText(pub.getPostalCode());
                GUI.getPublisherPhoneNumberTextField().setText(pub.getPhoneNumber());
            }
        }
    }
    
    public ListModel getboksOfPublisherListModel(Publisher get) {
        DefaultListModel lm = new DefaultListModel();
        for (Book book : get.getBook()) {
            
                lm.addElement(book);
            }
            
        if(lm.getSize()<=0){
            lm.addElement("NO Published book");
        }
        return lm;
    }
    
    private boolean checkInput(String name, String postalCode, String email, String phoneNUmber) {
        if (name == null || postalCode == null || email == null || phoneNUmber == null) {
            return false;
        }
        
        if (name.equals("") || postalCode.equals("") || email.equals("") || phoneNUmber.equals("")) {
            return false;
        }
        
        return true;
        
    }

    public String addPublisher(String name, String postalCode, String email, String phoneNUmber) {
        if (!checkInput(name, postalCode, email, phoneNUmber)) {
            return "Invalid Input";
        }
        Publisher pub = new Publisher(name, email, phoneNUmber, postalCode);
        bookShop.getPublishers().add(pub);
        bookShop.getGUI().getPublishersList().setModel(getPublishersListModel());
        bookShop.getGUI().getBookPublisherComboBox().setModel(getPublishersComboBoxModel());
        return "Saved";
    }

    public ComboBoxModel getPublishersComboBoxModel() {
         DefaultComboBoxModel cbm;
        
        cbm = new DefaultComboBoxModel();
        
        for (Publisher a : bookShop.getPublishers()) {
            
            cbm.addElement(a);
            
        }
        if (cbm.getSize() <= 0) {
            cbm.addElement("NO Publishers");
        }
        return cbm;
    }
    
}
